package com.qianyanglong.luping;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import javax.sound.sampled.AudioSystem;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;

import com.qianyanglong.luping.utils.LuPing;
import com.qianyanglong.luping.vo.ScreenInfo;

public class Application {
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Map<String,Object> map = new HashMap<String,Object>();
		JFrame jFrame = new JFrame("录屏软件");
		jFrame.setSize(415,300);
		jFrame.setLocationRelativeTo(null);
		jFrame.setResizable(false);
		jFrame.setLayout(null);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		int start = 10;
		
		JLabel screenLabel = new JLabel("屏幕:");
		screenLabel.setBounds(25, start, 60, 40);
		screenLabel.setFont(new Font("宋体", Font.BOLD, 18));
		jFrame.add(screenLabel);
		
		JComboBox<ScreenInfo> screenBox = new JComboBox<ScreenInfo>();
		GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice[] screenDevices = localGraphicsEnvironment.getScreenDevices();
		for (GraphicsDevice graphicsDevice : screenDevices) {
			Rectangle rect = graphicsDevice.getDefaultConfiguration().getBounds();
			String iDstring = graphicsDevice.getIDstring();
			screenBox.addItem(new ScreenInfo(iDstring,rect));
		}
		screenBox.setBounds(85, start , 290, 40);
		jFrame.add(screenBox);
		
		JLabel fileLabel = new JLabel("文件:");
		fileLabel.setBounds(25, start + 50, 60, 40);
//		Border border = BorderFactory.createLineBorder(Color.BLACK);
//		fileLabel.setBorder(border);
		fileLabel.setFont(new Font("宋体", Font.BOLD, 18));
		jFrame.add(fileLabel);
		
		JTextField fileTextField = new JTextField();
		fileTextField.setBounds(85, start + 50 , 290, 40);
//		fileTextField.setBorder(border);
		fileTextField.setFont(new Font("宋体", Font.BOLD, 18));
		jFrame.add(fileTextField);
		
		JButton startButton = new JButton("开始录制");
		startButton.setBounds(25,start + 100,100,40);
		jFrame.add(startButton);
		
		JButton pauseButton = new JButton("暂停录制");
		pauseButton.setBounds(150,start + 100,100,40);
		pauseButton.setEnabled(false);
		jFrame.add(pauseButton);
		
		JButton stopButton = new JButton("停止录制");
		stopButton.setEnabled(false);
		stopButton.setBounds(275,start + 100,100,40);
		jFrame.add(stopButton);
		
		startButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				
				Thread t = new Thread(new Runnable() {
					
					@Override
					public void run() {
						String path = fileTextField.getText();
						if(path==null||path.trim().length()==0) {
							JOptionPane.showMessageDialog(jFrame, "目录和文件名不能为空", "错误", JOptionPane.ERROR_MESSAGE);
							return;
						}
						File file = new File(path);
						if(!file.getParentFile().exists()) {
							JOptionPane.showMessageDialog(jFrame, "目录不存在", "错误", JOptionPane.ERROR_MESSAGE);
							return;
						}
						if(file.exists()) {
							int showConfirmDialog = JOptionPane.showConfirmDialog(jFrame, "文件已存在，是否覆盖?", "警告", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
							if(showConfirmDialog==JOptionPane.OK_OPTION) {
								file.delete();
							}else {
								return;
							}
						}
						map.put("luping", "1");
						startButton.setEnabled(false);
						pauseButton.setEnabled(true);
						stopButton.setEnabled(true);
						LuPing.luping(map, path);
					}
				});
				t.start();
			}
		});
		stopButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				
				Thread t = new Thread(new Runnable() {
					
					@Override
					public void run() {
						LuPing.stop();
						startButton.setEnabled(true);
						pauseButton.setEnabled(false);
						stopButton.setEnabled(false);
					}
				});
				t.start();
			}
		});
		jFrame.setVisible(true);
		
	}
}
